# -*- coding: utf-8 -*-

import urllib2
import socket
from lib.BeautifulSoup import BeautifulSoup

def getPage(url, timeout = 10):
    try:
        socket.setdefaulttimeout(timeout)
        u = urllib2.urlopen(url)
        content = decode(u.read())
        u.close()
        return BeautifulSoup(content)
    except:
        #printmsg(u'页面获取失败')
        return None
    
def getPageText(url, timeout=10):
    try:
        socket.setdefaulttimeout(timeout)
        u = urllib2.urlopen(url)
        content = decode(u.read())
        u.close()
        return content
    except Exception,e:
        #printmsg(u'页面获取失败')
        return None
    
def decode(str):
    result = None
    try:
        result = str.decode('gb2312')
        return result
    except:
        try:
            result = str.decode('gbk')
            return result
        except:
            try:
                result = str.decode('utf-8')
                return result
            except:
                return None
            
def printmsg(msg):
    print msg